﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using JESAI.Redis.Abstracts.Resolvers;
using JESAI.Redis.Consts;
using JESAI.Redis.Resolvers;
using JESAI.Redis.Datas;

namespace JESAI.Redis.Commands
{
    public partial class RedisCommand
    {
        public async Task<bool> SetAsync(string key, object value, TimeSpan expiryTime = default, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var argv = expiryTime == default ? new[] { key, value } : new[] { key, value, RedisCommandConst.Px, expiryTime == default ? 0 : expiryTime.TotalMilliseconds };
            var result = await _redisResolver.InvokeSimpleAsync(new RedisCommandArg(RedisCommandConst.Set, argv));
            return result == RedisCommandConst.Ok;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<RedisData> GetAsync(string key, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            return await _redisResolver.InvokeSimpleAsync(new RedisCommandArg(RedisCommandConst.Get, new object[] { key }, key));
        }
    }
}
